package com.mifengs.mall.ui.my;


import android.text.TextUtils;

import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.entity.RedPacket;
import com.mifengs.mall.entity.RedPacketBean;
import com.mifengs.mall.entity.ResponseInfo;
import com.mifengs.mall.event.RedPacketEvent;
import com.mifengs.mall.netWork.NetWork;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lihaoqi on 2018/1/26.
 */

public class RedPacketPresent implements RedPacketContract.Presenter {

    private RedPacketContract.View mView;

    public RedPacketPresent(RedPacketContract.View mView) {
        this.mView = mView;
    }


    @Override
    public void getRedPacketData(final int page, int type) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("status",type);
        hashMap.put("pageSize",Constant.PAGE_SIZE);
        hashMap.put("currentPage",page);
        if(!TextUtils.isEmpty(PreferenceUtils.getToken())){
            hashMap.put("app_token",PreferenceUtils.getToken());
        }
        NetWork.getRedPacketApi().getData(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseInfo<RedPacketBean>>bindToLife())
                .subscribe(new Observer<ResponseInfo<RedPacketBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showLongToast(e.toString());
                        if(page==1){
                            mView.setRefreshState(false);
                        }
                    }

                    @Override
                    public void onNext(ResponseInfo<RedPacketBean> redPacketBeanResponseInfo) {
                        if(page==1){
                            mView.setRefreshState(false);
                        }
                        if(redPacketBeanResponseInfo.getStatus()==Constant.OK_CODE){
                            RedPacketBean redPacketBean = redPacketBeanResponseInfo.getResult();
                            List<RedPacket> redPacketList = redPacketBean.getResultList();
                            int total = redPacketBean.getTotal();
                            boolean isLoadOver = total<=page*Constant.PAGE_SIZE?true:false;
                            if(page>1){
                                mView.setMoreData(redPacketList,isLoadOver);
                            }else{
                                mView.setData(redPacketList,isLoadOver);
                            }
                        }else if(redPacketBeanResponseInfo.getStatus()==-1){
                            mView.goToLogin();
                            mView.finish();
                        }else{
                            mView.showLongToast(redPacketBeanResponseInfo.getMsg());
                        }
                    }
                });
    }

    @Override
    public void revRedPacketData(final RedPacket redPacket) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("id",redPacket.getId());
        if(!TextUtils.isEmpty(PreferenceUtils.getToken())){
            hashMap.put("app_token",PreferenceUtils.getToken());
        }
        NetWork.getRedPacketApi().revRed(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseInfo>bindToLife())
                .subscribe(new Observer<ResponseInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showLongToast(e.toString());
                    }

                    @Override
                    public void onNext(ResponseInfo redPacketResponseInfo) {
                        if(redPacketResponseInfo.getStatus()==Constant.OK_CODE){
                            EventBus.getDefault().post(new RedPacketEvent(RedPacketEvent.REFRESH));
                            mView.showLongToast(redPacket.getCommissionAmount()+"元已存入到零钱账户中");
                        }else{
                            mView.showLongToast(redPacketResponseInfo.getMsg());
                        }
                    }
                });
    }


}
